﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PwjCy.Lite.Storage.Imp.SqlServer
{
    /// <summary>
    /// sqlserver 数据库连接
    /// </summary>
    public class Connection : AbstractConnection
    {
        private static IDbConnection _dbConn = new SqlConnection();
        internal Connection(IStorageContext storageContext)
            : base(storageContext, _dbConn)
        {
            _conn.ConnectionString = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};",
                storageContext.DataSource,
                storageContext.InitialCatalog,
                storageContext.UserID,
               _PasswordGenerateService.DecryptPassword(storageContext.Password)
                );
        }
        public override IStatement CreateStatement(string sql)
        {
            SqlCommand cmd = new SqlCommand()
            {
                CommandText = sql,
                Connection = (SqlConnection)_dbConn,
                CommandTimeout = _storageContext.ConnectionTimeout,
            };
            return new Statement(cmd);
        }
        public override DataTable GetOleDbSchemaTable(Guid schema, object[] restrictions)
        {
            throw new NotImplementedException();
        }
        public override string GetParameterPlaceHolder(string key)
        {
            return _conn.ConnectionString;
        }
        public override DataTable GetSchemaTables(string catalog, string schema, string name, string type)
        {
            throw new NotImplementedException();
        }
        public override ICallableStatement PrpareCall(string sql)
        {
            throw new NotImplementedException();
        }
        public override IPreparedStatement PrepareStatement(string sql)
        {
            SqlCommand cmd = new SqlCommand()
            {
                CommandText = sql,
                Connection = (SqlConnection)_dbConn
            };
            return new PreparedStatement(cmd);
        }
    }
}
